import Vue from 'vue'
import Router from 'vue-router'
import Cookie from 'js-cookie'

Vue.use(Router)
let routes = []
const routerContext = require.context('./', true, /index\.js$/)
routerContext.keys().forEach(route => {
	// 如果是根目录的index.js不处理
	console.log(route);
	if (route.startsWith('./main')) {
		return
	}
	const routerMoudule = routerContext(route)
	routes = [...routes, ...(routerMoudule.default || routerMoudule)]

})

routes.forEach(route => {
	route.path = route.path || '/' + (route.name || '');
});

const router = new Router({
	mode: 'hash',
	routes,
	scrollBehavior(to, from, savedPosition) {
		if (savedPosition) {
			return savedPosition
		} else {
			return {
				x: 0,
				y: 0
			}
		}
	}
});


// router.beforeEach((to, from, next) => {
// 	const token = Cookie.get('token');
// 	if (to.meta.noAuth) {
// 		next();
// 	} else {
// 		if (token) {
// 			next();
// 		} else {
// 			next({
// 				path: '/',
// 				query: {
// 					redirect: to.fullPath
// 				} // 将跳转的路由path作为参数，登录成功后跳转到该路由
// 			})
// 		}
// 	}
// 
// });



console.log('当前路由对象');
console.log(routes);


export default router
